// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
/* 引入qs */
import qs from 'qs'

Vue.use(ElementUI);
/* 设置代理的域名 */
axios.defaults.baseURL = 'http://localhost:8080';
/* 设置cookie,session跨域配置 */
axios.defaults.withCredentials = true;
/* 设置post请求体*/
axios.defaults.headers.post['Content-Type'] = 'application/json';
/* 设置全局axios写法 */
Vue.prototype.$http = axios
/* 设置全局qs写法 */
Vue.prototype.$qs = qs

Vue.config.productionTip = false

// 请求拦截器
axios.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = token  // 直接使用token，不加Bearer前缀
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
axios.interceptors.response.use(
  response => {
    return response.data  // 直接返回后端的响应数据
  },
  error => {
    if (error.response && error.response.status === 401) {
      localStorage.removeItem('token')
      router.push('/login')
    }
    return Promise.reject(error)
  }
)

/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
